
#pragma once

#include "UnitSpawn.h"
#include "hgerect.h"

class MapView;

class Map
{
public:

	// Constructor
	Map(MapView *_parent);
	Map(int _tWidth, int _tHeight);

	// Initializer
	void Initialize();

	// Loads the map from file
	bool LoadTerrain(const char *filePath);
	bool LoadScenario(const char *filePath);

	// Saves the map to file
	bool SaveTerrain(const char *filePath);
	bool SaveScenario(const char *filePath);

	// Sets the size of the terrain
	void SetTerrainSize(int width, int height);

	// Clears out the data
	void Clear();

	// Sets the new size of the terrain
	void Resize(int width, int height);

	// Set the terrain point at the x and y coordinate
	void SetTerrainPoint(hgeRect &viewArea, float x, float y, int t);

	// Calculates terrain index for 2D coordinate
	void CalculatePoint(hgeRect &viewArea, float x, float y, int &x0, int &y0);

	// Get terrain value
	int GetTerrain(int row, int col) const { return terrain[row*tWidth+col]; }
	// Set terrain value
	void SetTerrain(int row, int col, int value) { terrain[row*tWidth+col] = value; }
	// Reference to the cell
	int &Cell(int row, int col) { return terrain[row*tWidth+col]; }

	// Renders the terrain
	void Render(hgeRect &viewArea, bool drawAreaLines = false);

	// MapView pointer for easy access
	MapView *parent;

	// Unit data
	UnitSpawn unitSpawn;

private:

	// Terrain data
	int tWidth, tHeight;
	vector<int> terrain;
	int tileSet;
	hgeQuad tile;
	hgeRect terrainArea;
};